#!/usr/bin/python
# -*- coding: utf-8 -*-

#copy from:http://blog.doughellmann.com/2009/04/pymotw-multiprocessing-part-2.html
# 这个例子的说明没看得太懂，大概意思似乎是说某些情况不赞成使用这种方法，此例即演示这种情况。

import sys
import multiprocessing


def myprint(obj):
    sys.stdout.write(str(obj) + '\n')


def producer(ns, event):
    ns.my_list.append('This is the value')  # DOES NOT UPDATE GLOBAL VALUE!
    event.set()


def consumer(ns, event):
    myprint('Before event, consumer got: %s' % ns.my_list)
    event.wait()
    myprint('After event, consumer got: %s' % ns.my_list)


if __name__ == '__main__':
    mgr = multiprocessing.Manager()
    namespace = mgr.Namespace()
    namespace.my_list = []

    event = multiprocessing.Event()
    p = multiprocessing.Process(target=producer, args=(namespace,
                                event))
    c = multiprocessing.Process(target=consumer, args=(namespace,
                                event))

    c.start()
    p.start()

    c.join()
    p.join()
